home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / ZSI / wstools / TimeoutSocket.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-13  |  5KB  |  223 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. ident = '$Id: TimeoutSocket.py 237 2003-05-20 21:10:14Z warnes $'
  5. import string
  6. import socket
  7. import select
  8. import errno
  9. WSAEINVAL = getattr(errno, 'WSAEINVAL', 10022)
  10.  
  11. class TimeoutSocket:
  12.     
  13.     def __init__(self, timeout = 20, sock = None):
  14.         self.timeout = float(timeout)
  15.         self.inbuf = ''
  16.         if sock is None:
  17.             sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  18.         
  19.         self.sock = sock
  20.         self.sock.setblocking(0)
  21.         self._rbuf = ''
  22.         self._wbuf = ''
  23.  
  24.     
  25.     def __getattr__(self, name):
  26.         return getattr(self.sock, name)
  27.  
  28.     
  29.     def connect(self, *addr):
  30.         timeout = self.timeout
  31.         sock = self.sock
  32.         
  33.         try:
  34.             sock.setblocking(0)
  35.             apply(sock.connect, addr)
  36.             sock.setblocking(timeout != 0)
  37.             return 1
  38.         except socket.error:
  39.             why = None
  40.             if not timeout:
  41.                 raise 
  42.             
  43.             sock.setblocking(1)
  44.             if len(why.args) == 1:
  45.                 code = 0
  46.             else:
  47.                 (code, why) = why
  48.             if code not in (errno.EINPROGRESS, errno.EALREADY, errno.EWOULDBLOCK):
  49.                 raise 
  50.             
  51.             (r, w, e) = select.select([], [
  52.                 sock], [], timeout)
  53.             if w:
  54.                 
  55.                 try:
  56.                     apply(sock.connect, addr)
  57.                     return 1
  58.                 except socket.error:
  59.                     why = None
  60.                     if len(why.args) == 1:
  61.                         code = 0
  62.                     else:
  63.                         (code, why) = why
  64.                     if code in (errno.EISCONN, WSAEINVAL):
  65.                         return 1
  66.                     
  67.                     raise 
  68.                 except:
  69.                     None<EXCEPTION MATCH>socket.error
  70.                 
  71.  
  72.             None<EXCEPTION MATCH>socket.error
  73.  
  74.         raise TimeoutError('socket connect() timeout.')
  75.  
  76.     
  77.     def send(self, data, flags = 0):
  78.         total = len(data)
  79.         next = 0
  80.         while None:
  81.             (r, w, e) = select.select([], [
  82.                 self.sock], [], self.timeout)
  83.             if w:
  84.                 buff = data[next:next + 8192]
  85.                 sent = self.sock.send(buff, flags)
  86.                 next = next + sent
  87.                 if next == total:
  88.                     return total
  89.                     continue
  90.                 continue
  91.             
  92.             raise TimeoutError('socket send() timeout.')
  93.             continue
  94.             return None
  95.  
  96.     
  97.     def recv(self, amt, flags = 0):
  98.         if select.select([
  99.             self.sock], [], [], self.timeout)[0]:
  100.             return self.sock.recv(amt, flags)
  101.         
  102.         raise TimeoutError('socket recv() timeout.')
  103.  
  104.     buffsize = 4096
  105.     handles = 1
  106.     
  107.     def makefile(self, mode = 'r', buffsize = -1):
  108.         self.handles = self.handles + 1
  109.         self.mode = mode
  110.         return self
  111.  
  112.     
  113.     def close(self):
  114.         self.handles = self.handles - 1
  115.         if self.handles == 0 and self.sock.fileno() >= 0:
  116.             self.sock.close()
  117.         
  118.  
  119.     
  120.     def read(self, n = -1):
  121.         if not isinstance(n, type(1)):
  122.             n = -1
  123.         
  124.         if n >= 0:
  125.             k = len(self._rbuf)
  126.             if n <= k:
  127.                 data = self._rbuf[:n]
  128.                 self._rbuf = self._rbuf[n:]
  129.                 return data
  130.             
  131.             n = n - k
  132.             L = [
  133.                 self._rbuf]
  134.             self._rbuf = ''
  135.             while n > 0:
  136.                 new = self.recv(max(n, self.buffsize))
  137.                 if not new:
  138.                     break
  139.                 
  140.                 k = len(new)
  141.                 if k > n:
  142.                     L.append(new[:n])
  143.                     self._rbuf = new[n:]
  144.                     break
  145.                 
  146.                 L.append(new)
  147.                 n = n - k
  148.             return ''.join(L)
  149.         
  150.         k = max(4096, self.buffsize)
  151.         L = [
  152.             self._rbuf]
  153.         self._rbuf = ''
  154.         while None:
  155.             new = self.recv(k)
  156.             if not new:
  157.                 break
  158.             
  159.             k = min(k * 2, 1048576)
  160.             continue
  161.             return ''.join(L)
  162.  
  163.     
  164.     def readline(self, limit = -1):
  165.         data = ''
  166.         i = self._rbuf.find('\n')
  167.         while i < 0 and not None if limit < limit else limit <= len(self._rbuf):
  168.             new = self.recv(self.buffsize)
  169.             if not new:
  170.                 break
  171.             
  172.             i = new.find('\n')
  173.             if i >= 0:
  174.                 i = i + len(self._rbuf)
  175.             
  176.             self._rbuf = self._rbuf + new
  177.         if i < 0:
  178.             i = len(self._rbuf)
  179.         else:
  180.             i = i + 1
  181.         if limit <= limit:
  182.             pass
  183.         elif limit < len(self._rbuf):
  184.             i = limit
  185.         
  186.         data = self._rbuf[:i]
  187.         self._rbuf = self._rbuf[i:]
  188.         return data
  189.  
  190.     
  191.     def readlines(self, sizehint = 0):
  192.         total = 0
  193.         list = []
  194.         while None:
  195.             line = self.readline()
  196.             if not line:
  197.                 break
  198.             
  199.             total += len(line)
  200.             if sizehint and total >= sizehint:
  201.                 break
  202.                 continue
  203.             continue
  204.             return list
  205.  
  206.     
  207.     def writelines(self, list):
  208.         self.send(''.join(list))
  209.  
  210.     
  211.     def write(self, data):
  212.         self.send(data)
  213.  
  214.     
  215.     def flush(self):
  216.         pass
  217.  
  218.  
  219.  
  220. class TimeoutError(Exception):
  221.     pass
  222.  
  223.